home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / docs / hyper / IndexAG.lha / IndexAG / IndexAG.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-16  |  37.2 KB  |  1,048 lines

  1. /*
  2.    IndexAG 
  3.    AmigaGuide context index creator
  4.    By Sebastien Boisvert
  5.    Version 2 revision 0
  6.    
  7.    Description:
  8.    
  9.    Purpose: To create an 'Index' node for all words/links on an
  10.             AmigaGuide document in their context(s)
  11.             
  12.    Options:
  13.         Wordfile: list of words to ignore; one word per line,
  14.                   semi-conlon (;) as first chararcter are comments
  15.         
  16.         NOLINKS:  do not include internal/external links in Index node
  17.         
  18.         NOWORDS: do not incluce words in Index nodes
  19.         
  20.         Threshold: maximum times a word is allowed to repeat in a node
  21.                    before it is too repetitive and omitted
  22.         
  23.         COMMENT: for words that exceed the threshold, comment them so
  24.                  the author can decide to include it or not later
  25.         
  26.         GroupStart/GroupEnd: sequence of characters that determine the
  27.                              start/end of word groups
  28.    
  29.    General Operation: Reads the word list (if NOWORDS is not set). Opens
  30.         the document and proceeds to scan words/links within set parameters
  31.         and catalogs them. As they are catalogued, it is added to the list
  32.         for the node they are in, unless already present. Once done, 
  33.         produces the index in alphabetical order, check that the links are
  34.         valid as it goes along.
  35.         
  36. */
  37.  
  38. #define DEBUG 0
  39.  
  40. /* debug 1 = see each word/node/link as we go along */
  41. /* debug 2 = see each deallocation */
  42. /* debug >2 make a memory report */
  43.  
  44. #include <exec/memory.h>
  45. #include <exec/lists.h>
  46. #include <exec/nodes.h>
  47. #include <exec/types.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <ctype.h>
  52.  
  53. #include <proto/dos.h>
  54. #include <clib/dos_protos.h>
  55. #include <clib/alib_protos.h>
  56. #include <clib/exec_protos.h>
  57.  
  58. #include "strip.h"
  59.  
  60. #if DEBUG ==3
  61. # define MWDEBUG 1
  62. # include "sc:extras/memlib/memwatch.h"
  63. #endif
  64.  
  65. #define VERSIONNUMBER "2.1"
  66.  
  67. long __oslibversion = 37;
  68.  
  69. char VersionString[] = "$VER: IndexAG "VERSIONNUMBER" ©1998 by Sébastien Boisvert ("__DATE__")";
  70.  
  71. struct MinList *nodelist=NULL;  /* Points to the nodes list */
  72. struct MinList *linklist=NULL;  /* Points to the links list */
  73. struct MinList *exwords=NULL;   /* Points to the excluded words list */
  74.  
  75. struct treenode         /* tree structure */
  76. {
  77.         struct treenode *leftptr;   /* pointer to left */
  78.         struct word *text;                 /* contains pointer to direct name of link/word */
  79.         BOOL type;                  /* identifies it as a word (TRUE) or link (FALSE) */
  80.         struct treenode *rightptr;
  81. };
  82.  
  83. struct word     /* Word structure */
  84. {
  85.         struct MinNode nextword;
  86.         char    *name;          /* the string */
  87.         struct node *node;      /* the node it belongs to */
  88.         WORD   linenumber;    /* its linenumber in the node */
  89.         WORD   count;         /* the count */
  90.         
  91.         
  92.         
  93. };
  94.  
  95. struct node      /* Node structure */
  96. {
  97.         struct MinNode nextnode;
  98.         WORD   words;            /* number of words in node */
  99.         char   *title;             /* Node title string */
  100.         char  *name;             /* Node name string */
  101.         struct MinList *wordlist; /* pointer to its words */   
  102. };
  103.  
  104. struct link      /* Link structure */
  105. {
  106.         struct MinNode nextlink;
  107.         char *name;    /* the name */
  108.         char  *node;   /* the name of the node it references */
  109.         WORD   linenumber;    /* the line number associated with the link, if any */
  110.  
  111.         
  112.  
  113. };
  114.  
  115. struct xword
  116. {
  117.    struct MinNode nextxword;
  118.    char *word;
  119. };
  120.  
  121. UBYTE currentline;
  122. UBYTE currentnode;
  123. char buffer[513]="";
  124. UBYTE threshold;
  125. BOOL nowords,nolinks,comment;
  126. char groupstart[5]="";
  127. char groupend[5]="";
  128. char GuideName[80];
  129. char WordFile[80];
  130. BPTR masterfile;
  131. BPTR outfile;
  132. int spacing;
  133. int lines;
  134. struct treenode *treeptr = NULL;
  135. char alpha = 64;
  136.  
  137. void Freexwords(void);
  138. int readwords(void);
  139. int readarguments(void);
  140. int scannode(void);
  141. int scanword(char *, const struct node *);
  142. void cleanup(void);
  143. int doublex(const struct node*, char *);
  144.  
  145.  
  146. void __regargs __chkabort(void)   /* disables CTRL_C */
  147. {}
  148.  
  149. int readwords(void)
  150. {
  151.    void SortMinList(struct MinList *mlh);
  152.    BPTR wordfilehandle;
  153.    struct xword *word1;
  154.    void *temp;
  155.    
  156.    if(!(wordfilehandle=Open(WordFile,MODE_OLDFILE)))       /* Open our word file */
  157.    {
  158.      printf("Could not open word file '%s'!\n", WordFile);
  159.      return 0;
  160.    }
  161.    else
  162.    {
  163.       if (!(exwords=AllocMem(sizeof(struct MinList), MEMF_CLEAR)))      /* Allocate memory for the list header */
  164.         printf("Not enough mem for wordlist\n");
  165.       else
  166.       {
  167.         
  168.         NewList((struct List *)exwords);
  169.       
  170.         /* We start adding words here */
  171.       
  172.       FGets(wordfilehandle, buffer, 513);      /* Prime loop */
  173.       do
  174.       {
  175.          if (buffer[0] != ';')  /* If it's not a comment line */
  176.          {
  177.            if (!(word1=AllocMem(sizeof(struct xword),MEMF_CLEAR)))  /* Allocate space for the node */
  178.               printf("Not enough memory for newword structure\n");
  179.            else
  180.            {
  181.               if (!(temp=AllocMem(strlen(buffer), MEMF_CLEAR)))  /* Allocate for the word - buffer has extra \n !! */
  182.                  printf("Not enough memory to store word\n");
  183.               else
  184.               {
  185.                   word1->word = temp;                         /* point our word pointer to the memory allocated */
  186.                   buffer[(strlen(buffer)-1)] = '\0';               /* we change the \n to a NULL */
  187.                   strncpy(word1->word, buffer, strlen(buffer));
  188.                   AddTail((struct List *)exwords, (struct Node *)word1);  /* add it to the list */
  189.               }
  190.            }
  191.          }
  192.           /* get next word */
  193.       }while(FGets(wordfilehandle, buffer, 513));        /* repeat until done reading */
  194.       }
  195.    }
  196.    Close(wordfilehandle);
  197.  
  198.    SortMinList(exwords);
  199.    
  200.    return 1;
  201. }
  202.  
  203. /*------------------------------------------------------------------------*/
  204. /*                                                                        *
  205.  *  $Id: sortminlist.c 1.1 1996/09/15 07:42:23 heinz Exp $
  206.  *                                                                        */
  207. /*------------------------------------------------------------------------*/
  208.  
  209. /*------------------------------------------------------------------------*/
  210. /*
  211.  * A simple Insertion Sort based MinList sort function with a test case
  212.  * (C)1996 by Heinz Wrobel, <heinz@hwg.muc.de>
  213.  *
  214.  * Based on SAS/C 6.56. Use a command line like this to check this out:
  215.  *  SC $*.c LINK DEF=TEST
  216.  */
  217. /*------------------------------------------------------------------------*/
  218.  
  219. /*------------------------------------------------------------------------*/
  220. void SortMinList(struct MinList *mlh)
  221. {
  222.     struct xword *mn1, *mn2, *mnnext;
  223.  
  224.     /* We are doing a simple insertion like sort here, working our way
  225.      * sequentially through the list. The runtime should be acceptable
  226.      * for smaller lists. MinLists are used because this is the lowest
  227.      * common denominator for all lists. I always felt that it is better
  228.      * to cast something down than to cast it upwards.
  229.      */
  230.  
  231.     /* This makes sense only if we have more than one node in the list! */
  232.     mn1 = (struct xword *)mlh->mlh_Head;
  233.     if(mn1->nextxword.mln_Succ)
  234.     {
  235.         /* While there is still a current node to sort in, sort it in.
  236.          * We stash the next pointer right away to avoid losing it on
  237.          * node removal. Note how we also skip the very first node!
  238.          */
  239.         for(mn1 = (struct xword *)mn1->nextxword.mln_Succ;
  240.             mnnext = (struct xword *)mn1->nextxword.mln_Succ;
  241.             mn1 = mnnext)
  242.         {
  243.             /* Work our way backwards to find
  244.              * the correct insertion point
  245.              */
  246.             for(mn2 = (struct xword *)mn1->nextxword.mln_Pred;
  247.                 mn2->nextxword.mln_Pred;
  248.                 mn2 =(struct xword *) mn2->nextxword.mln_Pred)
  249.             {
  250.                 /* Our callback must return the same values
  251.                  * strcmp would return:
  252.                  *      <0  : mn1 <  mn2
  253.                  *      =0  : mn1 == mn2
  254.                  *      >0  : mn1 >  mn2
  255.                  * We stop when we find a node that is smaller than the
  256.                  * current one and insert our node after it
  257.                  */
  258.                 if(stricmp(mn1->word, mn2->word) > 0)
  259.                 {
  260.                     break;
  261.                 } /* if */
  262.             } /* for */
  263.  
  264.             /* Only do something, if we are not at
  265.              * the correct position yet
  266.              */
  267.             if(mn2->nextxword.mln_Succ != (struct MinNode *)mn1)
  268.             {
  269.                 /* Remember how we stashed the next pointer to continue! */
  270.                 Remove((struct Node *)mn1);
  271.                 Insert((struct List *)mlh,
  272.                        (struct Node *)mn1, (struct Node *)mn2);
  273.             } /* if */
  274.         } /* while */
  275.     } /* if */
  276.  
  277. } /* SortMinList */
  278.  
  279. /*------------------------------------------------------------------------*/
  280.  
  281.       
  282. void cleanup(void)   /* Here we free the entire xwords list */
  283. {
  284.    struct xword *new;
  285.    struct node *newnode;
  286.    struct word *newwrd;
  287.    struct link *newlink;
  288.    void *temp;
  289.    void freetree(struct treenode *);
  290.  
  291.    if(exwords)
  292.    {
  293. #if DEBUG ==2
  294.   printf("Freeing exwords\n");
  295. #endif      
  296.      for (new= (struct xword *)exwords->mlh_Head; new->nextxword.mln_Succ; new=(struct xword *)exwords->mlh_Head)
  297.      { 
  298. #if DEBUG ==2
  299.  printf(" Clearing word '%s' from exwords\n",new->word);
  300. #endif
  301.          if(new->word)
  302.           FreeMem(new->word, strlen(new->word)+1);   /* release size of word + the NULL */
  303.          temp=RemHead((struct List *)exwords);             /* remove node from list */
  304.          FreeMem(temp, sizeof(struct xword));              /* release memory */
  305.      }
  306. #if DEBUG ==2
  307.  printf("Freed exwords list\n\n");
  308. #endif
  309.      FreeMem(exwords, sizeof(struct MinList));  /* release the list */
  310.    }
  311.    
  312.   if(nodelist)  /* If there's a node list */
  313.   {
  314. #if DEBUG ==2
  315.  printf("Freeing nodelist\n");
  316. #endif
  317.    for(newnode =(struct node *)nodelist->mlh_Head; newnode->nextnode.mln_Succ; newnode=(struct node *)nodelist->mlh_Head)
  318.    {
  319. #if DEBUG ==2
  320.  printf(" Clearing node:title '%s' & nodename '%s'\n", newnode->title, newnode->name);
  321. #endif
  322.       if(newnode->title)
  323.        FreeMem(newnode->title, strlen(newnode->title)+1);  /* free its name and title */
  324.       if(newnode->name)
  325.        FreeMem(newnode->name, strlen(newnode->name)+1);
  326.       if(newnode->wordlist)   /* check for wordlist */
  327.       {
  328. #if DEBUG ==2
  329.  printf("  Freeing node's wordlist\n");
  330. #endif         
  331.          for(newwrd= (struct word *)newnode->wordlist->mlh_Head; newwrd->nextword.mln_Succ; newwrd= (struct word *)newnode->wordlist->mlh_Head)
  332.          {
  333. #if DEBUG ==2
  334.   printf("   Freeing word '%s' in node's wordlist with count %d, threshold is %d\n", newwrd->name, newwrd->count, threshold);
  335. #endif
  336.             if(newwrd->name)
  337.              FreeMem(newwrd->name, strlen(newwrd->name)+1);   /* clear each word node */
  338.             temp=RemHead((struct List *)newnode->wordlist);
  339.             FreeMem(temp, sizeof(struct word));
  340.          }
  341. #if DEBUG ==2
  342.  printf("   Freed node's wordlist\n");
  343. #endif      
  344.           FreeMem(newnode->wordlist, sizeof(struct MinList));  /* free wordlist */
  345.       }
  346. #if DEBUG ==2
  347.  printf("  Freed node\n");
  348.  #endif
  349.       temp=RemHead((struct List *)nodelist);  /* free the node */
  350.       FreeMem(temp, sizeof(struct node));
  351.    }
  352. #if DEBUG ==2
  353.  printf("Freed nodelist\n\n");
  354. #endif
  355.    FreeMem(nodelist, sizeof(struct MinList));   /* finally free the node list */
  356.   }
  357.    
  358.    if(linklist) /* check for link list */
  359.    {
  360. #if DEBUG ==2
  361.  printf("Freeing linklist\n");
  362. #endif
  363.       for(newlink = (struct link *)linklist->mlh_Head; newlink->nextlink.mln_Succ; newlink=(struct link *)linklist->mlh_Head)
  364.       {
  365. #if DEBUG ==2
  366.  printf("  Freeing link node name '%s' from linklist %d\n", newlink->name, newlink->linenumber);        
  367. #endif
  368.          if (newlink->name)
  369.              FreeMem(newlink->name, strlen(newlink->name)+1);  /* free the link name */
  370.          if(newlink->node)
  371.              FreeMem(newlink->node, strlen(newlink->node)+1); /* free the node name and finally the node */
  372.          temp=RemHead((struct List *)linklist);
  373. #if DEBUG ==2
  374.   printf(" Freeing node from linklist\n");
  375. #endif
  376.          FreeMem(temp, sizeof(struct link));
  377.       }
  378. #if DEBUG ==2
  379.  printf("Freed linklist\n");
  380. #endif      
  381.       FreeMem(linklist, sizeof(struct MinList));   /* free the list */
  382.    }
  383.    
  384.    if(treeptr)
  385.         freetree(treeptr);      /* free the tree */
  386.          
  387. }
  388.  
  389. void freetree(struct treenode *treeptr)  /* this recursively clears the tree */
  390. {
  391.         freetree(treeptr->leftptr);
  392.         freetree(treeptr->rightptr);
  393.         FreeMem(treeptr, sizeof(struct treenode));
  394. }
  395.  
  396. int readarguments(void)      /* read in arguments form CLI */
  397. {
  398.    int result=0;
  399.    struct RDArgs *rda;
  400.    LONG options[8];
  401.    
  402.    printf("IndexAG "VERSIONNUMBER" by Sébastien Boisvert\n");
  403.  
  404.      if ((rda = AllocDosObject(DOS_RDARGS, NULL)) != NULL)
  405.      {
  406.          rda->RDA_ExtHelp = "\nFILENAME/A   name of the AmigaGuide database\n"
  407.                             "WORDFILE/K   alternate word file to use\n"
  408.                             "THRESHOLD/N  maximum number of times a word can repeat in a node\n"
  409.                             "NOWORDS/S    do not index words, only links\n"
  410.                             "NOLINKS/S    do not index links, only words. Overrides NOWORDS if both are set\n"
  411.                             "COMMENT/S    comment words that cross threshold\n"
  412.                             "GROUPSTART/K the sequence of character(s) that indicate the start of a group\n"
  413.                             "GROUPEND/K   the sequence of character(s) that indicate the end of a group\n"
  414.                             " (Both GROUPSTART and GROUPEND must be no more than 4 characters)\n"
  415.                             "Enter arguments";
  416.       
  417.       
  418.         options[0] = (LONG)"";                 /* defaults */
  419.         options[1] = (LONG)"IndexAG.words";
  420.         options[2] = NULL;
  421.         options[3] = 0;
  422.         options[4] = 0;
  423.         options[5] = 0;
  424.         options[6] = (LONG)"<";
  425.         options[7] = (LONG)">";
  426.       
  427.          if (ReadArgs("FILENAME/A,WF=WORDFILE/K,TH=THRESHOLD/N,NW=NOWORDS/S,NL=NOLINKS/S,C=COMMENT/S,GS=GROUPSTART/K,GE=GROUPEND/K", options, rda) != NULL)
  428.         {
  429.            strcpy(GuideName,(char *)options[0]);
  430.            strcpy(WordFile, (char *)options[1]);
  431.            if (options[2] != NULL)
  432.              threshold = *(LONG *)options[2];
  433.            else
  434.               threshold = 10;
  435.            nowords = options[3];
  436.            nolinks = options[4];      
  437.            comment= options[5]; 
  438.            
  439.            if (nowords && nolinks)
  440.               nowords =0;
  441.            
  442.            strncpy(groupstart,(char *)options[6], 4);
  443.            strncpy(groupend,(char *)options[7], 4);
  444.            FreeArgs(rda);
  445.            FreeDosObject(DOS_RDARGS, rda);
  446.            
  447.            result =1;  /* success */
  448.         }
  449.         else
  450.            PrintFault(IoErr(), "IndexAG");
  451.            
  452.      return result;
  453.   }
  454. }  
  455.  
  456. int scannode()
  457. {
  458.    char temptitle[80]="";
  459.    char tempname[80]="";
  460.    char *temp;
  461.       char scannodebuffer[100]="";
  462.    char *tempbuffer=scannodebuffer;
  463.    char mid[5]="";
  464.    struct node *tempnode;
  465.    struct word *tempword, *searchdouble;
  466.    long i;
  467.    
  468.    strcpy(tempname, strtok(NULL, " \""));       /* get the name of the node */
  469.    
  470.    if(!(strncmp(tempname, "\"", 1)))            /* if it's in strings extract untill next quote */
  471.    {
  472.       strcpy(tempname, tempname+1);
  473.       strcat(tempname, " ");
  474.       strcat(tempname,(strtok(NULL, "\"")));
  475.    }
  476.    
  477.    if(!(strnicmp(tempname,"INDEX", 5)))         /* check if it's the INDEX node */
  478.      return 1;                                  /* and return (skip it) if so */
  479.    
  480.    strcpy(temptitle, strtok(NULL, "\n"));       /* get the title */
  481.    
  482.    strip(tempname, " \"\n", 'B');      
  483.    strip(temptitle, " \"", 'B');
  484. #if DEBUG ==1
  485.  printf("Found node:'%s'   '%s'\n", tempname, temptitle);
  486. #endif   
  487.          
  488.       if(!(tempnode=AllocMem(sizeof(struct node), MEMF_CLEAR)))
  489.       {
  490.         printf("Not enough memory to add node to the list!\n");
  491.         return 0;
  492.       }
  493.       else
  494.       {
  495.          AddTail((struct List *)nodelist, (struct Node *)tempnode);  /* add new node */
  496.          tempnode->name = NULL;
  497.          tempnode->title=NULL;
  498.          
  499.          if(!(temp=AllocMem(strlen(temptitle)+1, MEMF_CLEAR)))
  500.          {
  501.            printf("Can't allocate memory for node title!\n");
  502.            return 0;
  503.          }
  504.          else
  505.          {
  506.             #if DEBUG ==1
  507.               printf("Allocated node title\n");                 /* add title */
  508.             #endif
  509.             tempnode->title=temp;
  510.             strcpy(temp, temptitle);
  511.             
  512.             if(!(temp=AllocMem(strlen(tempname)+1, MEMF_CLEAR)))
  513.             {
  514.                printf("Can't allocate memory for node name!\n");
  515.                return 0;
  516.             }
  517.             else
  518.             {
  519.                #if DEBUG ==1
  520.                  printf("Allocated node name\n");       /* add name */
  521.                #endif
  522.                tempnode->name = temp;
  523.                strcpy(temp, tempname);
  524.                
  525.                tempnode->words=0;
  526.                tempnode->wordlist = NULL;       /* initialise its wordlist to NULL */
  527.                
  528.                
  529.                if (!nowords)
  530.                {
  531.                   if(!(temp=AllocMem(sizeof(struct MinList), MEMF_CLEAR)))
  532.                   {
  533.                      printf("Can't allocate words list for node!\n");
  534.                      return 0;
  535.                   }
  536.                   else
  537.                   {
  538.                      #if DEBUG ==1
  539.                        printf("Allocated wordlist for node\n");         /* create word list */
  540.                      #endif
  541.                      tempnode->wordlist=(struct MinList *)temp;
  542.                      
  543.                      NewList((struct List *)tempnode->wordlist);        /* initialise list */
  544.                   }
  545.                }
  546.                                
  547.                lines=0;
  548.                    
  549.                do
  550.                {
  551.                   FGets(masterfile, buffer, 513);       /* read next line */
  552.                   if(!(strnicmp(buffer, "@ENDNODE", 7)))        /* if end of node */
  553.                      return 1;                                  /* return for next */
  554.                         
  555.                   strip(buffer, "\n", 'E');
  556.                   for(i=0; buffer[i] != NULL; i++)      /* make sure each character is printable */
  557.                     if(!(isprint(buffer[i])))           /* to validate scanning w/ strtok() */
  558.                        buffer[i]=' ';
  559.                   
  560.                   strip(tempbuffer, " ", 'B');
  561.                                 
  562.                   lines++;
  563.                      
  564.                   temp=strtok(buffer, " ");
  565.                   
  566.                         
  567.                   do
  568.                   {
  569.                     if(temp)    
  570.                     {
  571.                      strcpy(tempbuffer, temp);
  572.                                           
  573.                      /* Check for group word */
  574.                                                                
  575.                      if(!nowords && strlen(groupstart) && strlen(groupend) )
  576.                      {
  577.                        strmid(tempbuffer,mid,1,strlen(groupstart));
  578.                        if(!(stricmp(mid, groupstart)))
  579.                        {
  580.                          strmid(tempbuffer,mid,strlen(tempbuffer)-strlen(groupend),strlen(groupend));
  581.                          if(stricmp(mid,groupend))
  582.                          {
  583.                              strcat(tempbuffer, " ");   /* get full group of words */
  584.                              strcat(tempbuffer, strtok(NULL,groupend));
  585.                          }
  586.                        }  
  587.                              
  588.                        strip(tempbuffer,groupstart, 'S');
  589.                        strip(tempbuffer,groupend, 'E');
  590.                      }
  591.                            
  592.                      if(!(strncmp(tempbuffer,"@", 1)))
  593.                       if(!(scanword(tempbuffer, tempnode)))  /* check if it's a command */
  594.                           return 0;     /* return to caller if failure for cleanup() */
  595.                              
  596.                      if(temp=strchr(tempbuffer, '@'))  /* if there's a tag left over */
  597.                        if(*(++temp) == '{')             /* strip it */
  598.                           *(--temp)='\0';
  599.                      
  600.                      /* get rid of extraneous characters */
  601.                            
  602.                      strip(tempbuffer,"!#$%^&*()[]{}|,<>?;=\"`~", 'A');
  603.                      strip(tempbuffer,".'+-_:/\\ ", 'B');
  604. #if DEBUG ==1
  605.                      printf("Current buffer is:'%s' %d\n", tempbuffer, strlen(tempbuffer));
  606. #endif
  607.                      /* if we want to add words */
  608.                      
  609.                      if (!nowords && strlen(tempbuffer)!=0)
  610.                      {
  611.                         if(!(doublex(tempnode, tempbuffer))) /* check if in exclude list */
  612.                         {
  613.                                 /* check if already in current wordlist */
  614.                           for(searchdouble= (struct word *)tempnode->wordlist->mlh_Head; searchdouble ; searchdouble= (struct word *)searchdouble->nextword.mln_Succ)
  615.                               if (searchdouble->name)
  616.                               {
  617.       
  618.                                 if(!(stricmp(tempbuffer, searchdouble->name)))
  619.                                    break; 
  620.                                    
  621.                               }
  622.                           
  623.                           
  624.                           /* if not add it to list */    
  625.                           if (!searchdouble)
  626.                           {
  627.                              if(!(tempword=AllocMem(sizeof(struct word), MEMF_CLEAR)))
  628.                              {
  629.                                 printf("Not enough memory to add word node to node's list\n");
  630.                                 return 0;
  631.                              }
  632.                              else
  633.                              {
  634.                                 tempword->name = NULL;
  635.                                 AddTail((struct List *)tempnode->wordlist, (struct Node *)tempword);
  636.  
  637.                                 if(!(temp=AllocMem(strlen(tempbuffer)+1, MEMF_CLEAR)))
  638.                                 {
  639.                                    printf("Not enough memory to add word to node's list!\n");
  640.                                    return 0;
  641.                                 }
  642.                                 else
  643.                                 {
  644.                                    tempword->name = temp;
  645.                                    strcpy(tempword->name,tempbuffer);
  646.                                    tempword->node = tempnode;
  647.                                    tempword->linenumber=lines;
  648.                                    tempword->count=1;
  649.                                    
  650.                                    if (spacing < strlen(tempword->name))
  651.                                         spacing = strlen(tempword->name);
  652. #if DEBUG==1
  653.   printf(" Added word '%s' to node's wordlist\n", tempword->name);
  654. #endif
  655.                                 }
  656.                              }
  657.                           }
  658.                           else
  659.                              ++(searchdouble->count);   /* otherwise increase its count */
  660.                         } 
  661.                      }
  662.                    }
  663.                   }
  664.                   while (temp=strtok(NULL, " "));  /* loop back with next token */
  665.                  }
  666.                      while(1);  /* loop until EOF */
  667.                   }
  668.             }
  669.          }
  670.    return 1;
  671. }
  672.  
  673. int doublex(const struct node *node, char *string)   /* checks if word is in exclude list  */
  674. {
  675.    struct xword *xword;
  676.    for(xword=(struct xword *)exwords->mlh_Head; xword->nextxword.mln_Succ; xword= (struct xword *)xword->nextxword.mln_Succ)
  677.       if(!(stricmp(xword->word, string)))
  678.         return 1;
  679.   
  680.    return 0;
  681. }                       
  682.  
  683.  
  684. int scanword(char *tempbuffer, const struct node *node)
  685. {
  686.    char linkname[80]="";
  687.    char command[80]="";
  688.    char nodelink[80]="";
  689.    struct link *newlink;
  690.    void *temp;
  691.    char *ptr;
  692.    int tempnum;
  693.    
  694.    
  695.    if(!(strncmp(tempbuffer, "@{\"",3))) /* check if it's a link */
  696.    {
  697.       
  698.       strcpy(linkname, tempbuffer+3);           /* yes, get link name */
  699.       if(linkname[strlen(linkname)-1]!='"')
  700.       {
  701.         strcat(linkname, " ");
  702.         strcat(linkname, strtok(NULL,"\""));
  703.       }
  704.       
  705.       strcpy(command, strtok(NULL, " "));  /* get link where it points to */
  706.       strcpy(nodelink, strtok(NULL, "}"));
  707.       strip(nodelink, " ", 'E');
  708.       
  709.       ptr=strrchr(nodelink, ' ');       /* find the last space character */
  710.       
  711.       tempnum=atoi(ptr);
  712.       
  713.       if(tempnum != 0)  /* check if there's a number at the end */
  714.         *ptr='\0';
  715.          
  716.       strip(linkname, " \"", 'B');
  717.       strip(nodelink, " \"", 'B');
  718.  
  719. #if DEBUG ==1
  720.  printf("\nLink found for string '%s' to node '%s' lineref: %d\n", linkname, nodelink, tempnum);
  721. #endif      
  722.  
  723.       if(!(stricmp(command,"link")) && !nolinks)  /* if it's a link */
  724.       {
  725.         int match=0;
  726.         for(newlink= (struct link *)linklist->mlh_Head; newlink->nextlink.mln_Succ && !match; newlink = (struct link *)newlink->nextlink.mln_Succ)
  727.         {
  728.            if(!(stricmp(newlink->name, linkname)))  /* see if it's in list */
  729.               match=1;
  730.         }
  731.  
  732.         if(!match)      /* if not in list add it to links list */
  733.         {
  734.            if(!(newlink=AllocMem(sizeof(struct link), MEMF_CLEAR)))
  735.            {
  736.               printf("Can't allocate memory to add link node!\n");
  737.               return 0;
  738.            }
  739.            else
  740.            {
  741.               AddTail((struct List *)linklist, (struct Node *)newlink);
  742.               newlink->name = NULL;
  743.               newlink->node = NULL;
  744.               
  745.               if(!(temp=AllocMem(strlen(linkname)+1, MEMF_CLEAR)))
  746.               {
  747.                  printf("Can't allocate memory for link name!\n");
  748.                  return 0;
  749.               }
  750.               else
  751.               {
  752.                  strcpy(temp, linkname);
  753.                  newlink->name = temp;
  754.                  
  755.                  if (spacing < strlen(linkname))
  756.                         spacing = strlen(linkname);
  757.                         
  758.                  if(!(temp=AllocMem(strlen(nodelink)+1, MEMF_CLEAR)))
  759.                  {
  760.                      printf("Can't allocate memory for link node!\n");
  761.                      return 0;
  762.                  }
  763.                  else
  764.                  {
  765.                         strcpy(temp, nodelink);
  766.                         newlink->node = temp;
  767.                         newlink->linenumber = tempnum;
  768.                  }
  769.               }
  770.            }
  771.         }
  772.       }
  773.      else   /* it's not an internal link, but a command; index as word (by returning to caller with name in buffer */
  774.       {
  775.         if(!nowords)
  776.         {
  777.                 strcpy(tempbuffer, linkname);
  778.                 return 1;
  779.         }
  780.       } 
  781.    }
  782.    else
  783.    {
  784.      if(!(strncmp(tempbuffer,"@{", 2)))  /* check if it's a tag */
  785.      {
  786.         char *temp;
  787.         
  788.         if(temp=strchr(tempbuffer,'}')) /* skip it and see if there's anything after it */
  789.           strcpy(tempbuffer, temp+1);
  790.           
  791.         if (strlen(tempbuffer) !=0 && tempbuffer[strlen(tempbuffer)-1] == '}')
  792.           *(strchr(tempbuffer, '@')) = '\0'; /* if there's something and it contains another */
  793.                                      /* command, stip that command */
  794.            
  795.      }
  796.      else
  797.      {
  798.         *tempbuffer='\0';  /* otherwise it's a command so skip entire line */
  799.         strtok(NULL,"\n");
  800.         lines--;                /* command lines are not counted in linking */
  801.      } 
  802.    }
  803.    
  804.    return 1;  /* success */
  805. }
  806.  
  807. int createindex(void)
  808. {
  809.         int insertnode (struct treenode **, struct word *, BOOL);
  810.         int writeindex(struct treenode *);
  811.                 
  812.         struct node *currentnode;
  813.         struct word *current;
  814.         struct link *currentlink;
  815.  
  816.         printf("\nCreating index node...\n");
  817.         
  818.          if(!nowords)
  819.           for(currentnode = (struct node *)nodelist->mlh_Head; currentnode->nextnode.mln_Succ ; currentnode = (struct node *)currentnode->nextnode.mln_Succ)
  820.                 for(current = (struct word *)currentnode->wordlist->mlh_Head; current->nextword.mln_Succ != NULL ; current = (struct word *)current->nextword.mln_Succ)
  821.                         if(!(insertnode(&treeptr, current,  TRUE)))
  822.                                 return 0;
  823.  
  824.          if(!nolinks)
  825.           for(currentlink = (struct link *)linklist->mlh_Head; currentlink->name ; currentlink = (struct link *)currentlink->nextlink.mln_Succ)
  826.                 if(!(insertnode(&treeptr, (struct word *)currentlink, FALSE)))
  827.                         return 0;
  828.                 
  829.          if(!(writeindex(treeptr)))
  830.                 return 0;             
  831.                 
  832.  return 1;       
  833.         
  834. }
  835.  
  836. int writeindex(struct treenode *tree)
  837. {
  838.         struct node *node;
  839.         int i, match;
  840.         
  841.              if (tree != NULL)
  842.              {
  843.                     if(!(writeindex(tree->leftptr)))
  844.                         return 0;
  845.  
  846.                     switch(tree->type)
  847.                     {
  848.                         case TRUE:
  849.                                 if(!nowords)
  850.                                 if(tree->text->count < threshold || comment)
  851.                                 {
  852.                                         if(toupper(tree->text->name[0]) > alpha)
  853.                                         {
  854.                                                 while(toupper(tree->text->name[0]) != alpha)
  855.                                                         alpha++;
  856.                                                 
  857.                                                 sprintf(buffer, "\n %c\n\n", alpha);
  858.                                                 FPuts(outfile, buffer);
  859.                                         }               
  860.  
  861.                                         FPutC(outfile, (tree->text->count < threshold ? ' ' : '*'));
  862.                                         sprintf(buffer, "@{\" %s \" link \"%s\" %d}", tree->text->name, tree->text->node->name, tree->text->linenumber);
  863.                                         FPuts(outfile, buffer);
  864.                                         for (i=0; i<(spacing-strlen(tree->text->name)); i++)
  865.                                                 FPutC(outfile, ' ');
  866.                                         sprintf(buffer, "%s\n",tree->text->node->title);
  867.                                         FPuts(outfile, buffer);
  868.                                 }
  869.                                 break;
  870.                         case FALSE:
  871.                         
  872.                                 if(!nolinks)
  873.                                 {       
  874.                                         if(toupper(tree->text->name[0]) > alpha)
  875.                                         {
  876.                                                 while(toupper(tree->text->name[0]) != alpha)
  877.                                                         alpha++;
  878.                                                 
  879.                                                 sprintf(buffer, "\n %c\n\n", alpha);
  880.                                                 FPuts(outfile, buffer);
  881.                                         }
  882.                                                        
  883.                                         for(node = (struct node *)nodelist->mlh_Head, match=0; node->nextnode.mln_Succ; node = (struct node *)node->nextnode.mln_Succ)
  884.                                                 if(!(stricmp(node->name, (char *)tree->text->node)))
  885.                                                 {
  886.                                                         match =1;
  887.                                                         break;
  888.                                                 }
  889.                                                 
  890.                                         if(match)       
  891.                                         {
  892.                                                 sprintf(buffer," @{\" %s \" link \"%s\" %d}", tree->text->name, node->name, (struct link *)tree->text->linenumber);
  893.                                                 FPuts(outfile, buffer);
  894.                                                 for (i=0; i<(spacing-(strlen(tree->text->name))); i++)
  895.                                                         FPutC(outfile, ' ');
  896.                                                 sprintf(buffer, "%s\n", node->title);
  897.                                                 FPuts(outfile,buffer);
  898.                                          }
  899.                                          else
  900.                                          {
  901.                                                 printf("  Can't find node for link \"%s\" - please double check reference.\n", tree->text->name); 
  902.                                                 sprintf(buffer, "?@{\" %s \" link \"%s\" %d}\n", tree->text->name, (char *)tree->text->node, tree->text->linenumber);
  903.                                                 FPuts(outfile, buffer);
  904.                                          }
  905.                                 }
  906.                                 break;
  907.                      }
  908.                      
  909.                 if(!(writeindex(tree->rightptr)))
  910.                         return 0;
  911.                 FreeMem(tree, sizeof(struct treenode));
  912.                 }
  913.    return 1;             
  914. }
  915.  
  916. int insertnode(struct treenode **tree, struct word *current, BOOL Type)
  917. {
  918.         if(*tree == NULL)
  919.         {
  920.                 *tree = AllocMem(sizeof(struct treenode), MEMF_CLEAR);
  921.                 
  922.                 if (*tree != NULL)
  923.                 {
  924.                         (*tree)->text = current;
  925.                         (*tree)->type = Type;
  926.                         (*tree)->leftptr = NULL;
  927.                         (*tree)->rightptr = NULL;
  928.                 }
  929.                 else
  930.                 {
  931.                         printf("Could not allocate memory to insert word in tree!\n");
  932.                         return 0;
  933.                 }
  934.         }
  935.         else
  936.         {
  937. #if DEBUG ==1   
  938. /*        printf("Comparing '%s' with '%s'\n", (*tree)->text->name, current->name); */
  939. #endif             
  940.                 if(stricmp((*tree)->text->name, current->name) >= 0)
  941.                         insertnode(&((*tree)->leftptr), current, Type);
  942.                 else
  943.                         insertnode(&((*tree)->rightptr), current, Type);
  944.         }                
  945.         return 1;
  946. }
  947.       
  948. void main(int argc, char *argv[])
  949. {
  950.     
  951.      char *tempword;
  952.      __aligned struct FileInfoBlock Fileinfo;
  953.      long filepos, filesize;
  954.      BPTR Filelock;
  955.      
  956.      if (argc == 0)
  957.         printf("This program must be run from the Shell.\n");
  958.      else
  959.      if(readarguments())
  960.      {
  961.         if (!(masterfile=Open(GuideName, MODE_OLDFILE)))
  962.             printf("Could not open file '%s'!\n", GuideName);
  963.         else
  964.         {
  965.            Filelock=Lock(GuideName,SHARED_LOCK);
  966.            Examine(Filelock, &Fileinfo);
  967.            UnLock(Filelock);
  968.            FGets(masterfile, buffer, 513);
  969.            filesize = Fileinfo.fib_Size;
  970.         
  971.            if (strnicmp(buffer, "@DATABASE", 9))
  972.               printf("Not an AmigaGuide Database!\n");
  973.            else
  974.            {
  975.               if (!nowords)
  976.                  readwords();
  977.                  
  978.               if(!nolinks)
  979.               {
  980.                   if(!(linklist=AllocMem(sizeof(struct MinList), MEMF_CLEAR)))
  981.                   {
  982.                     printf("Can't allocate node list structure!\n");
  983.                        goto exit;
  984.                   }
  985.                   else
  986.                     NewList((struct List *)linklist);
  987.                } 
  988.  
  989.               
  990.               if (!(nodelist=AllocMem(sizeof(struct MinList), MEMF_CLEAR)))
  991.                  printf("Not enough memory to create new nodelist!\n");
  992.               else
  993.               {
  994.                  NewList((struct List *)nodelist);
  995.  
  996.                  printf("\nScanning document...   ");                 
  997.                  FGets(masterfile, buffer, 513);
  998.                  
  999.                  do
  1000.                  {
  1001.                     tempword=strtok(buffer, " ");
  1002.                     if (!(strnicmp(tempword, "@NODE", 5)))
  1003.                     {
  1004.                       if(!scannode())
  1005.                           goto exit;
  1006.                           
  1007.                       filepos=Seek(masterfile,0,OFFSET_CURRENT);
  1008.                       printf("\b\b\b%2.0f%", (float)filepos / filesize * 100);
  1009.                       fflush(stdout);
  1010.  
  1011.                     }
  1012.                  }
  1013.                  while(FGets(masterfile, buffer, 513));    
  1014.                  
  1015.                  printf("\b\b\b   \n");
  1016.               }
  1017.               if(!(outfile = Open("index.node", MODE_NEWFILE)))
  1018.                         printf("Can't create \"index.guide\" for output!\n");
  1019.                else
  1020.                {
  1021.                     spacing+=3;
  1022.                     FPuts(outfile, "@NODE Index\n@REMARK This index created with IndexAG "VERSIONNUMBER" by Sébastien Boisvert\n\n");
  1023.                     if(!(createindex()))
  1024.                         printf("Could not create index!\n");
  1025.                     else
  1026.                     {
  1027.                         treeptr=NULL;
  1028.                         printf("\nDone!\n");
  1029.                     }
  1030.                     FPuts(outfile, "@ENDNODE\n");
  1031.                     Close(outfile);
  1032.                }
  1033.                
  1034.               exit:
  1035.                  cleanup();                 
  1036.            }
  1037.            Close(masterfile);
  1038.         }
  1039.         
  1040.       }
  1041.      
  1042. #if DEBUG >=3
  1043.     MWReport("IndexAG memory report", MWR_FULL);
  1044. #endif    
  1045.  
  1046. }
  1047.  
  1048.